package com.rongc.client.freight;


import android.content.Context;
import android.content.Intent;

import com.rongc.client.core.QuickApplication;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.base.model.BaseDic;
import com.rongc.client.freight.base.model.UserBean;
import com.rongc.client.freight.base.view.activity.HostActivity;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;

public class UniApplication extends QuickApplication {

    private UserBean userInfo;
    public static final String UPDATE_STATUS_ACTION = "com.umeng.message.example.action.UPDATE_STATUS";

    public static UniApplication getInstance() {
        return (UniApplication) QuickApplication.getInstance();
    }

    @Override
    public void onCreate() {
        super.onCreate();

        PushAgent mPushAgent = PushAgent.getInstance(this);
        //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回device token
                sendBroadcast(new Intent(UPDATE_STATUS_ACTION));
            }

            @Override
            public void onFailure(String s, String s1) {
            }
        });
//        mPushAgent.setPushIntentServiceClass(MyPushIntentService.class);
        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
            @Override
            public void dealWithCustomAction(Context context, UMessage msg) {
//                Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();
                ActivityUtils.startActivity(getInstance(), HostActivity.class);
            }
        };
        mPushAgent.setNotificationClickHandler(notificationClickHandler);
        /*mPushAgent.setMessageHandler(new UHandler() {
            @Override
            public void handleMessage(Context context, UMessage uMessage) {

                sendBroadcast(new Intent("com.rongc.client.freight"));
            }
        });*/
        mPushAgent.setDisplayNotificationNumber(10);

        initData();
    }

    private void initData() {
        getUserInfo();
    }

    public UserBean getUserInfo() {
        if (userInfo == null) {
            userInfo = UserBean.load();
            if(userInfo == null) userInfo = new UserBean();
        }
        return userInfo;
    }

    public boolean hasLogin(){
        getUserInfo();
        if (userInfo == null){
            return false;
        } else if(StringUtils.isEmpty(userInfo.getUserId())){
            return false;
        }else {
            return true;
        }
    }

    public boolean hasType(){
        getUserInfo();
        if (userInfo == null){
            return false;
        }
        // TODO 验证是否有身份
        else if(StringUtils.isEmpty(userInfo.getType())){
            return false;
        }else {
            return true;
        }
    }

    public String getType(){
        getUserInfo();
        if(BaseDic.IdentityType.DRIVING_TYPE.equals(userInfo.getType())){
            return BaseDic.IdentityType.DRIVING_TYPE;
        }else{
            return BaseDic.IdentityType.OWNER_TYPE;
        }
    }

    public boolean isAuth(){
        getUserInfo();
        return BaseDic.AuthStatus.AUTH_PASS_STATUS.equals(userInfo.getAuthStatus());
    }

    public boolean isAuthIng(){
        getUserInfo();
        return BaseDic.AuthStatus.AUTH_ING_STATUS.equals(userInfo.getAuthStatus());
    }
}